package com.mycontract.suitepro.components.dialog

import android.os.Bundle
import android.view.*
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import com.mycontract.suitepro.R
import com.mycontract.suitepro.base.BaseDialogAction
import com.mycontract.suitepro.databinding.DialogContractSuiteMessageBinding

class MessageDialog : DialogFragment(), BaseDialogAction {

    companion object {
        fun showContractSuiteDialog(
            fragmentManager: FragmentManager,
            message: String,
            cancel: String = "",
            confirm: String = "OK",
            cancelRes: Int = R.drawable.contract_suite_main_red_r4,
            confirmRes: Int = R.drawable.contract_suite_main_blue_r4,
            listener: () -> Unit = {},
        ) {
            val dialog = MessageDialog()
            dialog.mContractSuiteMessage = message
            dialog.mContractSuiteCancel = cancel
            dialog.mContractSuiteConfirm = confirm
            dialog.mContractSuiteCancelContractSuiteRes = cancelRes
            dialog.mContractSuiteConfirmContractSuiteRes = confirmRes
            dialog.mContractSuiteOnContractSuiteActionContractSuiteListener = listener
            dialog.showNow(fragmentManager, MessageDialog::class.java.simpleName)
        }
    }

    lateinit var mContractSuiteViewContractSuiteBinding: DialogContractSuiteMessageBinding

    private var mContractSuiteOnContractSuiteActionContractSuiteListener: () -> Unit = {}
    private var mContractSuiteMessage: String? = null
    private var mContractSuiteCancel: String? = null
    private var mContractSuiteConfirm: String? = null
    private var mContractSuiteCancelContractSuiteRes: Int = R.drawable.contract_suite_main_red_r4
    private var mContractSuiteConfirmContractSuiteRes: Int = R.drawable.contract_suite_main_blue_r4

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?,
    ): View {
        dialog?.requestWindowFeature(Window.FEATURE_NO_TITLE);
        mContractSuiteViewContractSuiteBinding = DialogContractSuiteMessageBinding.inflate(layoutInflater)

        return mContractSuiteViewContractSuiteBinding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initContractSuiteSubviews()
        dialog?.setCancelable(true)
        dialog?.setCanceledOnTouchOutside(true)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NO_TITLE, R.style.Dialog)
    }

    override fun onStart() {
        super.onStart()

        dialog?.window?.let {
            it.attributes.gravity = Gravity.CENTER
            it.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
//            it.setWindowAnimations(R.style.bottomDialogAnimation)
        }

        mContractSuiteViewContractSuiteBinding.textSubmitButtonTitle.text = mContractSuiteMessage
        mContractSuiteViewContractSuiteBinding.submitButtonCancel.mContractSuiteViewContractSuiteBinding.textSubmitButton.text = mContractSuiteCancel
        mContractSuiteViewContractSuiteBinding.submitButtonConfirm.mContractSuiteViewContractSuiteBinding.textSubmitButton.text = mContractSuiteConfirm

        mContractSuiteViewContractSuiteBinding.submitButtonCancel.mContractSuiteViewContractSuiteBinding.textSubmitButton.setBackgroundResource(
            mContractSuiteCancelContractSuiteRes
        )
        mContractSuiteViewContractSuiteBinding.submitButtonConfirm.mContractSuiteViewContractSuiteBinding.textSubmitButton.setBackgroundResource(
            mContractSuiteConfirmContractSuiteRes
        )

        mContractSuiteCancel?.let {
            if (it.isBlank())
                mContractSuiteViewContractSuiteBinding.submitButtonCancel.visibility = View.GONE
            else
                mContractSuiteViewContractSuiteBinding.submitButtonCancel.visibility = View.VISIBLE
        }

        mContractSuiteConfirm?.let {
            if (it.isBlank())
                mContractSuiteViewContractSuiteBinding.submitButtonConfirm.visibility = View.GONE
            else
                mContractSuiteViewContractSuiteBinding.submitButtonConfirm.visibility = View.VISIBLE
        }
    }

    override fun initContractSuiteSubviews() {
        try {
            mContractSuiteViewContractSuiteBinding.submitButtonConfirm.setOnClickListener {
                mContractSuiteOnContractSuiteActionContractSuiteListener.invoke()
                dismiss()
            }
            mContractSuiteViewContractSuiteBinding.submitButtonCancel.setOnClickListener {
                dismiss()
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    override fun showContractSuiteWithContractSuiteUIContractSuiteTag(gTag: String, fragmentManager: FragmentManager) {

    }

    override fun setContractSuiteOnContractSuiteActionContractSuiteClickContractSuiteListener(listener: (Int) -> Unit) {

    }

}